<!doctype html>
<script src="../../../resources/ahem.js"></script>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../assert_selection.js"></script>
<script>
// Test clicking on the left of RTL text puts the caret at the end of the line.

function testClickLeft(lineNumber, numberOfLines, width, sample, expected) {
    const kLineHeight = 20;
    const kStyle = [
        'direction: rtl;',
        `font: 10px/${kLineHeight}px Ahem;`,
        'line-break: normal;',
        `width: ${width}ch;`,
    ].join('');
    selection_test(
        [
            `<style>div {${kStyle}}</style>`,
            `<div contenteditable><bdo dir="rtl">${sample}</bdo></div>`,
        ],
        selection => {
            const target = selection.document.querySelector('div');
            if (!window.eventSender)
                throw 'This test requires eventSender.';
            assert_equals(target.offsetHeight, numberOfLines * kLineHeight);
            const offsetY = (lineNumber - 1)  * kLineHeight;
            eventSender.leapForward(9999); // reset mouse click
            eventSender.mouseMoveTo(
                selection.computeLeft(target) + 3,
                selection.computeTop(target) + offsetY + kLineHeight / 2);
            eventSender.mouseDown();
            eventSender.mouseUp();
        },
        [
            `<style>div {${kStyle}}</style>`,
            `<div contenteditable><bdo dir="rtl">${expected}</bdo></div>`,
        ],
        `Click left at line ${lineNumber}/${numberOfLines}`);
}

// 2 lines
testClickLeft(1, 2, 2, 'A BC', 'A| BC');
testClickLeft(2, 2, 2, 'A BC', 'A BC|');

// 3 lines
testClickLeft(1, 3, 4, 'AB CDEF GHI', 'AB| CDEF GHI');
testClickLeft(2, 3, 4, 'AB CDEF GHI', 'AB CDEF| GHI');
testClickLeft(3, 3, 4, 'AB CDEF GHI', 'AB CDEF GHI|');

// 5 lines
testClickLeft(1, 5, 4, 'AB CDE FGH IJK LMN', 'AB| CDE FGH IJK LMN');
testClickLeft(2, 5, 4, 'AB CDE FGH IJK LMN', 'AB CDE| FGH IJK LMN');
testClickLeft(3, 5, 4, 'AB CDE FGH IJK LMN', 'AB CDE FGH| IJK LMN');
testClickLeft(4, 5, 4, 'AB CDE FGH IJK LMN', 'AB CDE FGH IJK| LMN');
testClickLeft(5, 5, 4, 'AB CDE FGH IJK LMN', 'AB CDE FGH IJK LMN|');
</script>
